/////////////////////////////////////////////////////////////////////
// DBMessageHandler.h
//
//
// 11/16/2010
//
/////////////////////////////////////////////////////////////////////

#ifndef __PERSISTMSGHANDLER_H__
#define __PERSISTMSGHANDLER_H__

#include <map>
#include "Message/MsgHandlerIf.h"
#include "Database/DBMysql.h"
#include "Service/Service.h"

class MsgHandlerInitDataDB  : public IMsgHandlerInitData
{
public:
	MsgHandlerInitDataDB(Service* pSvcLgc):wpSvcLgc(pSvcLgc){}
	Service* wpSvcLgc;
	string db_server;
	string db_name;
	string db_user;
	string db_password;
	short db_port;
};



class DBMessage;
class PersistService;
class DBMessageHandler : public IMsgHandler
{
	friend PersistService;
public:
	DBMessageHandler( );
	virtual ~DBMessageHandler( );

	virtual bool initialize(IMsgHandlerInitData* data) ;

	virtual bool handleMsg( IMsg * message );
private:
	virtual void addSubHandler(int nMsgCate, int nMsgID, IMsgHandler* pHandler);
	virtual void rmvSubHandler(int nMsgCate, int nMsgID);
private:
	typedef std::map< int, IMsgHandler* > MessageMap;
	typedef std::map< int, IMsgHandler* >::iterator MessageMapIter;
	typedef std::map< int, IMsgHandler* >::const_iterator MessageMapConstIter;

	static MessageMap ms_MessageMap;

protected:
	static DBMysql m_DBMysql;

	static Service*	m_wpSvcLgc;
};

extern INT64	sqlResult;

#define SQL_QUERY( sql, ... )						\
	do												\
	{												\
		char strSQL[ 1024 ];							\
		sprintf_s( strSQL, 1024, sql, __VA_ARGS__ );	\
		sqlResult = m_DBMysql.Query( strSQL );		\
	} while ( 0 );

#endif // __PERSISTMSGHANDLER_H__